Skip to content

Commit

Permalink
vx: make exports of a module use its main entry as an external
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 10, 2021
1 parent ddfa9fd commit e60e9a6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/vast/types/vast.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
declare function createState(
onStateChange?: (...args: unknown[]) => unknown
): TCreateStateReturn;
type TStateInput<S> = S | (() => S);
type TStateInput<S> = S | ((prevState?: S) => S);
type TSetStateInput<S> = S | ((prevState: S) => S);
type TState = ReturnType<typeof createState>;
type TStateHandlerReturn<S> = [S, (nextState: TSetStateInput<S>) => void];
Expand Down
39 changes: 32 additions & 7 deletions vx/config/rollup/rollup.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ const isWatchModeOn = JSON.parse(process.env.ROLLUP_WATCH ?? false);
module.exports = cleanupConfig(
concatTruthy(!isWatchModeOn && opts.env.PRODUCTION, opts.env.DEVELOPMENT).map(
env => {
const packageName = usePackage();

const customConfigPath = vxPath.packageConfigPath(
usePackage(),
'vx.build.js'
packageName,
opts.fileNames.VX_BUILD
);

let customConfig;
Expand All @@ -36,11 +38,12 @@ module.exports = cleanupConfig(
}

return [].concat(
genBaseConfig({ env }),
genBaseConfig({ env, packageName }),
genExportsConfig(usePackage(), env),
customConfig?.({
getInputFile,
getPlugins: (options = {}) => getPlugins({ env, ...options }),
getPlugins: (options = {}) =>
getPlugins({ env, packageName, ...options }),
genOutput: (options = {}) => genOutput({ env, ...options }),
}) ?? []
);
Expand All @@ -55,12 +58,12 @@ function cleanupConfig(configs) {
.map(({ input, output, plugins }) => ({ input, output, plugins }));
}

function genBaseConfig({ env, moduleName = usePackage() }) {
function genBaseConfig({ env, packageName, moduleName = usePackage() }) {
return {
env,
input: getInputFile(moduleName),
output: genOutput({ env, moduleName }),
plugins: getPlugins({ env, moduleName }),
plugins: getPlugins({ env, moduleName, packageName }),
};
}

Expand Down Expand Up @@ -107,7 +110,8 @@ function getInputFile(moduleName = usePackage()) {

function getPlugins({
env = opts.env.PRODUCTION,
moduleName = usePackage(),
packageName = usePackage(),
moduleName = packageName,
} = {}) {
const plugins = [
replace({
Expand All @@ -119,6 +123,27 @@ function getPlugins({
},
}),
ts({
tsconfig: resolvedConfig => {
if (packageName === moduleName) {
return resolvedConfig;
}

// Make the package itself an external in the exported modules
// so that it can be imported in the generated code.

const modified = {
...resolvedConfig,
paths: {
...resolvedConfig.paths,
},
};

// This makes the package itself an external module
// since there is no relative path to it
delete modified.paths[packageName];

return modified;
},
browserList: ['IE10'],
hook: {
outputPath: (path, kind) => {
Expand Down
1 change: 1 addition & 0 deletions vx/opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = {
JEST_SETUP: 'jest.setup.ts',
JEST_SETUP_AFTER_ENV: 'jest.setupAfterEnv.ts',
MAIN_EXPORT: 'index.js',
VX_BUILD: 'vx.build.js',
},
format: {
UMD: 'umd',
Expand Down

0 comments on commit e60e9a6

Please sign in to comment.