-
-
Notifications
You must be signed in to change notification settings - Fork 534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add note about usage of ESM in source config file #1394
base: docs
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,29 @@ When multiple patterns match the same file, the last pattern takes precedence. | |
* `esm` overrides matches files to compile and execute as native ECMAScript modules. | ||
* `package` resets either of the above to default behavior, which obeys `package.json` `"type"` and `tsconfig.json` `"module"` options. | ||
|
||
**Note:** This does not mean that you have to write CommonJS in your source `webpack.config.ts` file | ||
- here you can continue writing imports and exports in ESM syntax. | ||
|
||
For example, this syntax with `import` and `export` in your `webpack.config.ts` source file is just fine: | ||
|
||
```ts | ||
import * as path from 'path'; | ||
import * as webpack from 'webpack'; | ||
// in case you run into any typescript error when configuring `devServer` | ||
import 'webpack-dev-server'; | ||
|
||
const config: webpack.Configuration = { | ||
mode: 'production', | ||
entry: './foo.js', | ||
output: { | ||
path: path.resolve(__dirname, 'dist'), | ||
filename: 'foo.bundle.js', | ||
}, | ||
}; | ||
|
||
export default config; | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hesitate to duplicate this rather than linking to webpack's documentation. webpack might be updating it soon. Also, webpack is not the only tool we want to integrate with. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about we create a webpack page under "Recipes"? That way, we can afford to give very specific webpack advice and examples, and we can link to the moduleTypes page. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, if we just follow your other suggestion from above and link to the webpack config, would that resolve this in a different way? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess the only thing we'd be saying that webpack's website isn't, is we'd be giving an example There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, I think I understand - you mean because if it was a recipe, both the |
||
|
||
## Caveats | ||
|
||
Files with an overridden module type are transformed with the same limitations as [`isolatedModules`](https://www.typescriptlang.org/tsconfig#isolatedModules). This will only affect rare cases such as using `const enum`s with [`preserveConstEnums`](https://www.typescriptlang.org/tsconfig#preserveConstEnums) disabled. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about wordsmithing this a bit to focus on "syntax" and avoid negatives, so we're saying something like "you can still use ECMAScript import / export syntax in your
.ts
file."Following that by a link to webpack's documentation page makes sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about merging this entire page into the "CommonJS vs native ECMAScript modules" page? Maybe giving that page a better title, like "Module Formats: CommonJS and ECMAScript modules"?
I'm wondering if we can have a single page that comprehensively describes the differences between CommonJS and ECMAScript modules, explains that you can almost always use
import
andexport
syntax, and explainsmoduleTypes
overrides.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, I think these two together makes for a really nice change. Then anyone confused can read everything about the module formats, because the information is right next to it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok cool. If I get to this before you, I think I'll take a stab at some of this refactoring -- pushing my changes to this branch -- and ask you for a review to make sure it's all still intuitive.