-
Notifications
You must be signed in to change notification settings - Fork 109
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 context option to webpack config #479
base: main
Are you sure you want to change the base?
Changes from all commits
3acee5c
796e165
bc968e2
2505bda
343b762
6b1728c
71ba528
9389d4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ import { PackageCache } from '@embroider/shared-internals'; | |
import { Memoize } from 'typescript-memoize'; | ||
import makeDebug from 'debug'; | ||
import { ensureDirSync, symlinkSync, existsSync } from 'fs-extra'; | ||
import VirtualModulesPlugin from 'webpack-virtual-modules'; | ||
|
||
const debug = makeDebug('ember-auto-import:webpack'); | ||
|
||
|
@@ -88,6 +89,8 @@ export default class WebpackBundler extends Plugin implements Bundler { | |
|
||
private lastBuildResult: BuildResult | undefined; | ||
|
||
private virtualModules; | ||
|
||
constructor(priorTrees: InputNode[], private opts: BundlerOptions) { | ||
super(priorTrees, { | ||
persistentOutput: true, | ||
|
@@ -127,8 +130,12 @@ export default class WebpackBundler extends Plugin implements Bundler { | |
entry[bundle] = [ | ||
join(stagingDir, 'l.js'), | ||
join(stagingDir, `${bundle}.js`), | ||
// If I understand correctly, our virtual modules will need to go in here... | ||
// `./__ember_auto_import__/l.js`, | ||
// `./__ember_auto_import__/${bundle}.js`, | ||
]; | ||
}); | ||
|
||
let config: Configuration = { | ||
mode: | ||
this.opts.environment === 'production' ? 'production' : 'development', | ||
|
@@ -172,7 +179,9 @@ export default class WebpackBundler extends Plugin implements Bundler { | |
), | ||
}, | ||
module: { | ||
noParse: (file: string) => file === join(stagingDir, 'l.js'), | ||
// I guess webpack should also be prevented from parsing our virtual modules, so presumably we could | ||
// filter them out like this? | ||
noParse: (file: string) => file === join(stagingDir, 'l.js') || file === `./__ember_auto_import__/l.js`, | ||
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. Yes, and this would now only need to match the l.js in the virtual modules.
|
||
rules: [ | ||
this.babelRule(stagingDir), | ||
{ | ||
|
@@ -196,6 +205,10 @@ export default class WebpackBundler extends Plugin implements Bundler { | |
}, | ||
node: false, | ||
externals: this.externalsHandler, | ||
// Here we add our virtual modules as per https://github.com/sysgears/webpack-virtual-modules | ||
plugins: [ | ||
this.virtualModules | ||
] | ||
}; | ||
|
||
mergeConfig( | ||
|
@@ -232,7 +245,10 @@ export default class WebpackBundler extends Plugin implements Bundler { | |
// | ||
// And we otherwise defer to the `skipBabel` setting as implemented by | ||
// `@embroider/shared-internals`. | ||
return dirname(filename) !== stagingDir && shouldTranspile(filename); | ||
return dirname(filename) !== stagingDir | ||
// I presume we also don't want to apply babel to our virtual modules, which I guess could be done as follows... | ||
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. 👍 |
||
&& filename.indexOf('/__ember_auto_import__/')<0 | ||
&& shouldTranspile(filename); | ||
}, | ||
use: { | ||
loader: 'babel-loader-8', | ||
|
@@ -289,10 +305,27 @@ export default class WebpackBundler extends Plugin implements Bundler { | |
async build(): Promise<void> { | ||
let bundleDeps = await this.opts.splitter.deps(); | ||
|
||
// Build our virtual modules which we'll pass into the webpack plugins array | ||
let virtualModulesHash = {}; | ||
for (let [bundle, deps] of bundleDeps.entries()) { | ||
virtualModulesHash[`./__ember_auto_import__/${bundle}.js`] = entryTemplate({ | ||
staticImports: deps.staticImports, | ||
dynamicImports: deps.dynamicImports, | ||
dynamicTemplateImports: | ||
deps.dynamicTemplateImports.map(mapTemplateImports), | ||
staticTemplateImports: | ||
deps.staticTemplateImports.map(mapTemplateImports), | ||
publicAssetURL: this.opts.publicAssetURL, | ||
}); | ||
} | ||
virtualModulesHash[`./__ember_auto_import__/l.js`] = loader; | ||
this.virtualModules = new VirtualModulesPlugin(virtualModulesHash); | ||
|
||
for (let [bundle, deps] of bundleDeps.entries()) { | ||
this.writeEntryFile(bundle, deps); | ||
} | ||
this.writeLoaderFile(); | ||
|
||
this.linkDeps(bundleDeps); | ||
let stats = await this.runWebpack(); | ||
this.lastBuildResult = this.summarizeStats(stats, bundleDeps); | ||
|
@@ -391,6 +424,7 @@ export default class WebpackBundler extends Plugin implements Bundler { | |
packageName: string; | ||
packageRoot: string; | ||
}): void { | ||
// I guess this is "part 2" of what stagingDir is being used for. Not yet sure how to replace this part... | ||
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. Yes. The files like You could instead use resolve.alias or resolve.fallback. See https://webpack.js.org/configuration/resolve/ This is probably a small amount of code to write, but it could have confusing effects on people who were already doing some aliasing. The alternative I mentioned in #479 (comment) is to actually locate the virtual modules such that they appear to be inside the real package that did the importing, so that the existing dependency resolution would work. This would be very nice because it means there's no confusion about aliases and we'd be closer to how a "normal" webpack build would work (one where webpack looks at the actual app source, instead of ember-auto-import's summary of the app source). This might need wider code changes though, so that you could know the needed imports on a per-package (meaning the app and any addons that are using ember-auto-import) basis. |
||
ensureDirSync(dirname(join(this.stagingDir, 'node_modules', packageName))); | ||
if (!existsSync(join(this.stagingDir, 'node_modules', packageName))) { | ||
symlinkSync( | ||
|
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.
Correct, the virtual modules would replace the existing
join(stagingDir, ...)
lines.