-
Notifications
You must be signed in to change notification settings - Fork 55
feat!: add shims config #291
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c76377c
fix: do not use CJS methods in ESM
fi3ework a3c6688
up
fi3ework 3629336
up
fi3ework b06d25d
up
fi3ework 7929ba9
up
fi3ework 8930ba6
up
fi3ework 00ac75b
up
fi3ework 76eb4f0
up
fi3ework c13bada
up
fi3ework c916741
up
fi3ework File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
registry = 'https://registry.npmjs.org/' | ||
strict-peer-dependencies=false | ||
hoist-patterns[]=[] | ||
hoist-patterns[]=[] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { type RsbuildPlugin, rspack } from '@rsbuild/core'; | ||
|
||
const importMetaUrlShim = `/*#__PURE__*/ (function () { | ||
return typeof document === 'undefined' | ||
? new (module.require('url'.replace('', '')).URL)('file:' + __filename).href | ||
: (document.currentScript && document.currentScript.src) || | ||
new URL('main.js', document.baseURI).href; | ||
})()`; | ||
|
||
// This Rsbuild plugin will shim `import.meta.url` for CommonJS modules. | ||
// - Replace `import.meta.url` with `importMetaUrl`. | ||
// - Inject `importMetaUrl` to the end of the module (can't inject at the beginning because of `"use strict";`). | ||
// This is a short-term solution, and we hope to provide built-in polyfills like `node.__filename` on Rspack side. | ||
export const pluginCjsImportMetaUrlShim = (): RsbuildPlugin => ({ | ||
name: 'rsbuild-plugin-cjs-import-meta-url-shim', | ||
fi3ework marked this conversation as resolved.
Show resolved
Hide resolved
|
||
setup(api) { | ||
api.modifyEnvironmentConfig((config) => { | ||
config.source.define = { | ||
...config.source.define, | ||
'import.meta.url': importMetaUrlShim, | ||
}; | ||
}); | ||
}, | ||
}); | ||
|
||
const requireShim = `// Rslib ESM shims | ||
import __rslib_shim_module__ from 'module'; | ||
const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(import.meta.url); | ||
`; | ||
|
||
export const pluginEsmRequireShim = (): RsbuildPlugin => ({ | ||
name: 'rsbuild-plugin-esm-require-shim', | ||
fi3ework marked this conversation as resolved.
Show resolved
Hide resolved
|
||
setup(api) { | ||
api.modifyRspackConfig((config) => { | ||
config.plugins ??= []; | ||
config.plugins.push( | ||
new rspack.BannerPlugin({ | ||
banner: requireShim, | ||
// Just before minify stage, to perform tree shaking. | ||
stage: rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE - 1, | ||
raw: true, | ||
include: /\.(js|cjs)$/, | ||
}), | ||
); | ||
}); | ||
}, | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.